#include "DataViewDelegate.h"



#include <ItemModel.h>
#include <ItemReference.h>



#include <QLineEdit>
#include <QComboBox>



DataViewDelegate::DataViewDelegate(QObject *parent) :
    QItemDelegate(parent)
{
}



QWidget* DataViewDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    Q_UNUSED(option);
    if (!index.isValid()) return 0;

    QModelIndex typeIndex = index.sibling(index.row(), AbstractItem::CategoryData);
    int type = typeIndex.data(Qt::EditRole).toInt();
    if (IS_REFERENCE_TYPE(type))
    {
        switch(index.column())
        {
        case ItemReference::ReferenceData:
//        case ItemReference::ReferenceType:
            return new QComboBox(parent);
        }
        return 0;
    }


    switch (index.column())
    {
    case AbstractItem::NameData:
    case Item::ValueData:
        return new QLineEdit(parent);
    }

    return 0;
}



void DataViewDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
    if (!index.isValid() || !editor) return;

    QModelIndex typeIndex = index.sibling(index.row(), AbstractItem::CategoryData);
    int type = typeIndex.data(Qt::EditRole).toInt();
    if (!IS_REFERENCE_TYPE(type))
    {
        QLineEdit* le = qobject_cast<QLineEdit*>(editor);
        if (!le) return;
        le->setText(index.data(Qt::EditRole).toString());
        return;
    }



    QComboBox* cb = qobject_cast<QComboBox*>(editor);
    if (!cb) return;


    int field = index.column();
    if (field == ItemReference::ReferenceData)
    {
        const QAbstractItemModel* model = index.model();
        if (!model) return;

        QModelIndex groupIndex = model->index(0, AbstractItem::IdData, QModelIndex());
        while (groupIndex.isValid())
        {
            QString groupName = itemData(groupIndex, AbstractItem::NameData, Qt::EditRole).toString();
            QVariant id = groupIndex.data(Qt::EditRole);
            cb->addItem(groupName, id);


            // todo: collect all child items recursively
            QModelIndex itemIndex = groupIndex.child(0, AbstractItem::IdData);
            while (itemIndex.isValid())
            {
                QVariant id = itemIndex.data(Qt::EditRole).toString();
                QString name = QString("%1 (%2)").arg(itemData(itemIndex, AbstractItem::NameData, Qt::EditRole).toString()).arg(groupName);
                cb->addItem(name, id);

                itemIndex = itemIndex.sibling(itemIndex.row()+1, itemIndex.column());
            }

            groupIndex = groupIndex.sibling(groupIndex.row()+1, groupIndex.column());
        }

        return;
    }


}



void DataViewDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
    if (!editor || !model || !index.isValid()) return;

    QModelIndex typeIndex = index.sibling(index.row(), AbstractItem::CategoryData);
    int type = typeIndex.data(Qt::EditRole).toInt();
    if (!IS_REFERENCE_TYPE(type))
    {
        QLineEdit* le = qobject_cast<QLineEdit*>(editor);
        if (!le) return;

        model->setData(index, le->text(), Qt::EditRole);
        return;
    }

    QComboBox* cb = qobject_cast<QComboBox*>(editor);
    if (!cb) return;

    model->setData(index, cb->itemData(cb->currentIndex()), Qt::EditRole);
}



QVariant DataViewDelegate::itemData(const QModelIndex &baseIndex, const int &type, const int &role) const
{
    if (!baseIndex.isValid()) return -1;

    QModelIndex index = baseIndex.sibling(baseIndex.row(), type);
    if (!index.isValid()) return -1;

    QVariant data = index.data(role);
    if (!data.isValid() || data.isNull()) return -1;

    return data;
}





